package com.parking.stall.service.consumer;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.parking.common.core.model.StateData;
import com.parking.stall.dao.StallCheckMapper;
import com.parking.stall.dao.StallInfoMapper;
import com.parking.stall.dao.StallRentTimeMapper;
import com.parking.stall.entity.StallCheck;
import com.parking.stall.entity.StallInfo;
import com.parking.stall.entity.StallRentTime;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * AddStallInfoConsumer
 * 车位审核通过：新增车位信息
 */
@Component
public class AddStallInfoConsumer {
    /**
     * stallInfoMapper 车位信息mapper
     * stallCheckMapper 车位审核mapper
     * stallRentTimeMapper 车位出租时间mapper
     */
    @Autowired
    private StallInfoMapper stallInfoMapper;
    @Autowired
    private StallCheckMapper stallCheckMapper;
    @Autowired
    private StallRentTimeMapper stallRentTimeMapper;
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(value="AddStallInfoRabbitMQ")})
    public void testRabbitMQ(HashMap hashMap) {
        StallCheck stallCheck = (StallCheck)hashMap.get("stallCheck");
        StallInfo stallInfo = (StallInfo)hashMap.get("stallInfo");
        stallInfo.setStallInfoAffiliation(StateData.STALL_IS_RENTERS);
        stallInfoMapper.insert(stallInfo);
        stallCheckMapper.update(null,new UpdateWrapper<StallCheck>()
                .eq("stall_check_id",stallCheck.getStallCheckId())
                .set("stall_info_id",stallInfo.getStallInfoId()));
        StallRentTime stallRentTime = new StallRentTime();
        stallRentTime.setStallInfoId(stallInfo.getStallInfoId());
        stallRentTime.setStallInfoNum(stallInfo.getStallInfoNum());
        stallRentTime.setStallInfoArea(stallInfo.getStallInfoArea());
        stallRentTime.setStallOwnerType(StateData.STALL_IS_RENTERS);
        stallRentTime.setStallOwner("出租客");
        stallRentTimeMapper.insert(stallRentTime);
    }
}
